package com.example.mgsnew.listeners;

import com.example.common.constants.MqConstants;
import com.example.common.constants.TakeConstants;
import com.example.common.domain.Messages.Mgs;
import com.example.mgsnew.Handler.NotificationHandler;
import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.AmqpRejectAndDontRequeueException;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class NotificationListener {
    @Autowired
    private NotificationHandler notificationHandler;

    @RabbitListener(queues = MqConstants.QUEUE_BROADCAST)
    public void handleTeacherNotification(Mgs mgs) throws JsonProcessingException {
        System.out.println("教师websocket发布通知: "+mgs.getType());
        try {
            // 根据通知类型进行不同处理
            switch (mgs.getType()) {
                case TakeConstants.SIGN_IN:
                    notificationHandler.sendNotificationToAllStudents(mgs);
                    break;
                case TakeConstants.HOMEWORK:
                    notificationHandler.sendHomework(mgs);
                    break;
                default:
                    System.err.println("未知类型通知: " + mgs.getType());
            }
        } catch (Exception e) {
            log.error("广播通知失败", e);
            throw new AmqpRejectAndDontRequeueException("广播失败", e);
        }
    }
}
